package com.hrm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrm.dto.Response;
import com.hrm.dto.ResponseDTO;
import com.hrm.entity.StaffLeave;
import com.hrm.entity.StaffRoom;
import com.hrm.enums.AuditStatusEnum;
import com.hrm.mapper.StaffLeaveMapper;
import com.hrm.mapper.StaffRoomMapper;
import com.hrm.util.HutoolExcelUtil;
import com.hrm.vo.StaffLeaveVO;
import com.hrm.vo.StaffRoomVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author qiujie
 * @since 2022-04-05
 */
@Service
public class StaffRoomService extends ServiceImpl<StaffRoomMapper, StaffRoom> {

    @Resource
    private StaffRoomMapper staffRoomMapper;

    public ResponseDTO add(StaffRoom staffRoom) {
        if (save(staffRoom)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO deleteById(Integer id) {
        if (removeById(id)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO deleteBatch(List<Integer> ids) {
        if (removeBatchByIds(ids)) {
            return Response.success();
        }
        return Response.error();
    }

    public ResponseDTO findById(Integer id) {
        StaffRoom staffRoom = getById(id);
        if (staffRoom != null) {
            return Response.success(staffRoom);
        }
        return Response.error();
    }


    public ResponseDTO list(Integer current, Integer size, String name) {
        IPage<StaffRoomVO> config = new Page<>(current, size);
        if (name == null) {
            name = "";
        }
        IPage<StaffRoomVO> page = this.staffRoomMapper.listStaffRoomVO(config, name);
        // 将响应数据填充到map中
        Map map = new HashMap();
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Response.success(map);
    }

//    public ResponseDTO listByStatus(Integer current, Integer size, String name) {
//        IPage<StaffRoomVO> config = new Page<>(current, size);
//        if (name == null) {
//            name = "";
//        }
//        IPage<StaffRoomVO> page = this.staffRoomMapper.listByStatus(config, name);
//        // 将响应数据填充到map中
//        Map map = new HashMap();
//        map.put("pages", page.getPages());
//        map.put("total", page.getTotal());
//        map.put("list", page.getRecords());
//        return Response.success(map);
//    }

    /**
     * 数据导出
     *
     * @param response
     * @return
     */
    public ResponseDTO export(HttpServletResponse response) throws IOException {
        List<StaffRoom> list = list();
        HutoolExcelUtil.writeExcel(response, list, "员工会议申请记录表", StaffRoom.class);
        return Response.success();
    }

    public ResponseDTO findByStaffId(Integer current, Integer size, Integer id) {
        IPage<StaffRoomVO> config = new Page<>(current, size);
        IPage<StaffRoomVO> page = this.staffRoomMapper.listStaffRoomVOByStaffId(config, id);
        // 将响应数据填充到map中
        Map map = new HashMap();
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Response.success(map);
    }

    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO edit(StaffRoom staffRoom) {
        if (updateById(staffRoom)) {
            return Response.success();
        }
        return Response.error();
    }

    /**
     * 查找未我的参会信息
     *
     * @param id
     * @return
     */
    public ResponseDTO findMeetingByDeptId(Integer current, Integer size, Integer id) {
        IPage<StaffRoomVO> config = new Page<>(current, size);
        IPage<StaffRoomVO> page = this.staffRoomMapper.listStaffRoomVOByDeptId(config, id);
        // 将响应数据填充到map中
        Map map = new HashMap();
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Response.success(map);
    }

    public ResponseDTO listMeeting(Integer current, Integer size, String name) {
        IPage<StaffRoomVO> config = new Page<>(current, size);
        if (name == null) {
            name = "";
        }
        IPage<StaffRoomVO> page = this.staffRoomMapper.listStaffRoomVOByStatus(config, name);
        // 将响应数据填充到map中
        Map map = new HashMap();
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Response.success(map);
    }
}




